<template>
  <div class="pagination-container">
    <el-pagination
      background
      layout="prev, pager, next,total,sizes"
      v-bind="$attrs"
      :total="total"
      :page-sizes="pageSizes"
      :page-size.sync="pageSize"
      :current-page.sync="currentPage"
      @size-change="handleUpdateSize"
      @current-change="handleUpdateCurrent"
    />
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: {
      type: Number,
      require: true,
      default: 0
    },
    limit: {
      type: Number,
      require: true,
      default: 10
    },
    page: {
      type: Number,
      require: true,
      default: 1
    }
  },
  data() {
    return {
      pageSizes: [10, 20, 50, 100]
    }
  },
  computed: {
    currentPage: {
      set(val) {
        console.log('update', val)
        this.$emit('update:page', val)
      },
      get() {
        console.log('get', this.page)
        return this.page
      }
    },
    pageSize: {
      get() {
        console.log('getLimit', this.limit)
        return this.limit
      },
      set(val) {
        console.log('updateLimit', val)
        this.$emit('update:limit', val)
      }
    }
  },
  created() {
    console.log(this.$attrs)
  },
  methods: {
    handleUpdateSize(val) {
      this.currentPage = 1
      this.pageSize = val
      this.$emit('pagination', { page: 1, limit: val })
    },
    handleUpdateCurrent(val) {
      console.log(this.pageSize)
      this.currentPage = val
      this.$emit('pagination', { page: val, limit: this.pageSize })
    }
  }
}
</script>

<style lang="scss">
.pagination-container{
  .el-pagination{
    padding: 0;
  }
  .el-pagination.is-background .btn-next, .el-pagination.is-background .btn-prev, .el-pagination.is-background .el-pager li{
    margin: 1px;
  }
}
</style>
